package com.yict.csms.company.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.common.util.DateUtil;
import com.yict.csms.company.entity.Contract;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Repository
public class ContractDaoImpl extends CommonDao<Contract, Long> {

	public List<Contract> getContractSelectMap(String id) {
		List<Contract> list = new ArrayList<Contract>();
		Date date = new Date();
		String str = DateUtil.DateToString(date, 1);
		String queryString = "";
		if(id==null||id.equals("")){
			queryString = "from Contract as c where c.effectiveDate <= to_date('"+str+"','yyyy-mm-dd') and c.expirationDate >= to_date('"+str+"','yyyy-mm-dd') order by c.createDate desc";
		}else{
		queryString = "from Contract as c where c.company.companyId = '"
				+ id + "' and c.effectiveDate <= to_date('"+str+"','yyyy-mm-dd') and c.expirationDate >= to_date('"+str+"','yyyy-mm-dd') order by c.createDate desc";
		}
		try {
			list = this.getTemplate().find(queryString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean valiContractNo(String contractNO) {
		boolean bool = false;
		String queryString = "Select count(*) from Contract as c where lower(c.contractNo) ='" + ( contractNO == null ? null
				: contractNO.toLowerCase() ) + "'";
		try {
			Query query = this.getSession().createQuery(queryString);
			Long i = (Long)query.uniqueResult();
			if(i==0)
				bool = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	public Contract findById(Long id){
		return (Contract)this.getTemplate().get(Contract.class, id);
	}
}
